package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.api.mongo.FriendApi;
import com.tanhua.db.UserInfo;
import com.tanhua.mongo.Comment;
import com.tanhua.mongo.Friend;
import com.tanhua.template.HuanXinTemplate;
import com.tanhua.vo.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageService {
    @Reference
    private FriendApi friendApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    //加为好友
    public ResponseEntity contactUser(Long friendId) {
        // 1、保存数据到MongoDB
        friendApi.save(UserHolder.getUser().getId(), friendId);
        // 2、好友关系注册到环信
        huanXinTemplate.addContacts(UserHolder.getUser().getId(), friendId);
        return ResponseEntity.ok(null);
    }

    public ResponseEntity contactsList(Integer page, Integer pagesize) throws InvocationTargetException, IllegalAccessException {
        PageBeanVo pageBeanVo = friendApi.contactsListByUserId(page, pagesize, UserHolder.getUser().getId());
        List<Friend> items = (List<Friend>) pageBeanVo.getItems();
        List<ContactVo> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(items)) {
            for (Friend friend : items) {
                UserInfo userInfo = userInfoApi.findByUserId(friend.getFriendId());
                ContactVo contactVo = new ContactVo();
                BeanUtils.copyProperties(userInfo,contactVo);
                contactVo.setUserId(userInfo.getId().toString());
                contactVo.setCity(userInfo.getCity().split("-")[0]);
                list.add(contactVo);
            }
        }
        pageBeanVo.setItems(list);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findPage(Integer pageNum, Integer pageSize, int i) {
        PageBeanVo pageBeanVo = commentApi.findPage(pageNum, pageSize, i,UserHolder.getUser().getId());
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();
        ArrayList<UserCommentVo> userCommentList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(items)){
            for (Comment comment : items) {
                UserCommentVo userCommentVo = new UserCommentVo();
                UserInfo userInfo = userInfoApi.findByUserId(comment.getUserId());
                BeanUtils.copyProperties(userInfo,userCommentVo);
                userCommentVo.setCreateDate(DateUtil.format(new Date(comment.getCreated()),"yyyy-MM-dd HH:mm:ss"));
                userCommentList.add(userCommentVo);
            }
        }
        pageBeanVo.setItems(userCommentList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
